
package TelasPedido;

import DAO.ClienteDAO;
import DAO.ItemDAO;
import DAO.NotaFiscalDAO;
import DAO.PedidoDAO;
import Pojo.Cliente;
import Pojo.Item;
import Pojo.Pedido;
import TelaCliente.TelaCadastroClienteFrame;
import View.MenuPedido;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


public class TelaNovoPedido extends javax.swing.JPanel {
    MenuPedido menuPedido = new MenuPedido();
    ItemDAO itemDao;
    NotaFiscalDAO notaFiscalDao;
    ClienteDAO clienteDao;
    PedidoDAO pedidoDao;
    Pedido pedido;
    
    public TelaNovoPedido() {
        initComponents();
    }
    public TelaNovoPedido( ItemDAO itemDao,ClienteDAO clienteDao, PedidoDAO pedidoDao,NotaFiscalDAO notaFiscalDao ){
        this();
        this.pedido = new Pedido();
        this.pedido.setCodigo(pedidoDao.proximoCodigo());
        this.itemDao = itemDao;
        this.clienteDao = clienteDao;
        this.pedidoDao = pedidoDao;
        this.notaFiscalDao = notaFiscalDao;
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jTextField3 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        jLabel1.setText("Produto");

        jLabel2.setText("Quantidade");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Produto", "Quantidade", "Codigo", "Preco"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTable1MousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton2.setText("Adicionar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Salvar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Limpar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });

        jLabel3.setText("CPF do Cliente");

        jButton5.setText("Finalizar ");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel4.setText("Itens no carrinho");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(84, 84, 84)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addGap(36, 36, 36)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addComponent(jButton4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(110, 110, 110)
                                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(2, 2, 2)))
                .addGap(79, 79, 79))
            .addGroup(layout.createSequentialGroup()
                .addGap(207, 207, 207)
                .addComponent(jLabel4)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addGap(5, 5, 5)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton5))
                .addContainerGap(13, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
            try{
                DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
                Item item = itemDao.buscaItemNome(jTextField1.getText() );
                if(item==null){
                    JOptionPane.showMessageDialog(null,"Item Não encontrado");
                }
                else if(item.getQuantidade() < Integer.parseInt(jTextField2.getText()) ){
                    JOptionPane.showMessageDialog(null,"Quantidade insuficiente de itens");
                }
                else{
                    menuPedido.CadastrarItem(pedido,itemDao, jTextField1.getText(), Integer.parseInt(jTextField2.getText()));
                    String [] linha = new String[]{jTextField1.getText() ,jTextField2.getText(),Integer.toString(item.getCodigo()),
                    Float.toString(Integer.parseInt(jTextField2.getText())*item.CalcularPreco())};
                    modelo.addRow(linha);
                    jTable1.setModel(modelo);
                }
            }catch(Exception e){
                JOptionPane.showMessageDialog(null,"Reveja os dados");
            }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        jTextField1.setText("");
        jTextField2.setText("");
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
            TelaCadastroClienteFrame telaCadastroCliente = new TelaCadastroClienteFrame(clienteDao,jTextField3.getText());
            DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
            Cliente aux = clienteDao.buscaCpf(jTextField3.getText());
            if(aux==null){
                Object[] options = {"Sim", "Não"}; 
                int i = JOptionPane.showOptionDialog(null,"Cliente não cadastrado, deseja cadastrar??", "ATENÇÃO", 
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,options, options[0]);
                if(i==0){
                telaCadastroCliente.setVisible(true);
                }
            }else if(modelo.getRowCount()==0){
                JOptionPane.showMessageDialog(null,"Nenhum item adicionado!");
            }
            else{
                pedido.setCliente(aux);
                pedidoDao.adicionarPedido(pedido);
                pedidoDao.escreverArquivo();
                this.pedido = new Pedido();
                this.pedido.setCodigo(pedidoDao.proximoCodigo());
                if (modelo.getRowCount() > 0){
                    modelo.setNumRows(0);
                    jTextField1.setText("");
                    jTextField2.setText("");
                    jTextField3.setText("");
                    JOptionPane.showMessageDialog(null,"Pedido adicionado!");
                }
            }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
            
            TelaCadastroClienteFrame telaCadastroCliente = new TelaCadastroClienteFrame(clienteDao,jTextField3.getText());
            DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
            Cliente aux = clienteDao.buscaCpf(jTextField3.getText());
            if(aux==null){
                JOptionPane.showMessageDialog(null,"Cliente Não Consta No Cadastro!");
                telaCadastroCliente.setVisible(true);
            }else if(modelo.getRowCount()==0){
                JOptionPane.showMessageDialog(null,"Nenhum item adicionado!");
            }
            else{
                pedido.setCliente(aux);
                if(menuPedido.fecharPedido(pedidoDao, pedido, notaFiscalDao,itemDao)){
                this.pedido = new Pedido();
                this.pedido.setCodigo(pedidoDao.proximoCodigo());
                if (modelo.getRowCount() > 0){  
                    modelo.setRowCount(0);
                    jTextField1.setText("");
                    jTextField2.setText("");
                    jTextField3.setText("");
                    JOptionPane.showMessageDialog(null,"Nota Fiscal emitida!");
                }
                }else{
                    JOptionPane.showMessageDialog(null,"Nao há produtos suficientes no estoque!");
                }
                    
            }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jTable1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MousePressed
        // TODO add your handling code here:
        jTable1.editingCanceled(null); 
    }//GEN-LAST:event_jTable1MousePressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
